<?php
/**
 * created by zzy
 * date: 2018/1/16 10:48
 */
namespace App\Eloquent\Ygt;

use Illuminate\Database\Eloquent\SoftDeletes;

class BillLog extends DbEloquent
{
    use SoftDeletes;
    protected $dates    = ['deleted_at'];
    protected $table    = 'ygt_bill_log';
    public static $tableFieldList   = [
        ['name'=>'id','title'=>'','value'=>0,'is_must'=>0],
        ['name'=>'company_id','title'=>'登录用户异常','value'=>0,'is_must'=>1],
        ['name'=>'type','title'=>'类型','value'=>0,'is_must'=>0,'type'=>1],
        ['name'=>'title','title'=>'名称','value'=>'','is_must'=>0],
        ['name'=>'bill_category_id','title'=>'分类','value'=>0,'is_must'=>1,'type'=>1],
        ['name'=>'bill_account_pay_id','title'=>'付款账户','value'=>0,'is_must'=>1,'type'=>1],
        ['name'=>'bill_account_id','title'=>'收款账户','value'=>0,'is_must'=>1,'type'=>1],
        ['name'=>'bill_buyer_id','title'=>'往来单位','value'=>0,'is_must'=>1,'type'=>1],
        ['name'=>'img_id','title'=>'图片','value'=>0,'is_must'=>0,'type'=>1],
        ['name'=>'money','title'=>'金额','value'=>0,'is_must'=>1],
        ['name'=>'remark','title'=>'备注','value'=>'','is_must'=>0,],
        ['name'=>'table','title'=>'','value'=>'','is_must'=>0,],
        ['name'=>'table_id','title'=>'','value'=>0,'is_must'=>0,],
        ['name'=>'customer_id','title'=>'客户id','value'=>0,'is_must'=>0,],
    ];
    //取type的配置列表
    public static function getTypeList($params='')
    {
        $lists              = [
            ['id'=>1,'title'=>'支出'],['id'=>2,'title'=>'收入'],
            ['id'=>3,'title'=>'应付'],['id'=>4,'title'=>'应收'],
        ];
        if($params == 'out'){
            //支出
            $result         = [1,3];
        }else if($params == 'in'){
            //收入
            $result         = [2,4];
        }else{
            $result         = $lists;
        }
        return $result;
    }
    public static function getInfoDetail($companyId,$id)
    {
        $where              = [
            'ygt_bill_log.company_id'=>$companyId,
            'ygt_bill_log.id'=>$id
        ];
        $limit              = $offset = '';
        $isList             = false;
        $result             = self::getListDetail($where,$limit,$offset,$isList);
        return $result;
    }
    public static function getWeekTextArr()
    {
        $result             = [
            '1'=>'周一','2'=>'周二','3'=>'周三','4'=>'周四','5'=>'周五','6'=>'周六','7'=>'周日',
        ];
        return $result;
    }
    //取模板列表
    public static function getListDetail($where,$limit,$offset,$isList=true)
    {

        $columns            = 'ygt_bill_log.id,ygt_bill_log.type,ygt_bill_log.title';
        $columns            .= ',ygt_bill_log.bill_category_id,ygt_bill_log.bill_account_id';
        $columns            .= ',ygt_bill_log.bill_buyer_id,ygt_bill_log.money,ygt_bill_log.img_id';
        $columns            .= ',ygt_bill_log.created_at';
        $columns            .= ',ygt_bill_category.title as bill_category_name';
        $columns            .= ',ygt_bill_account.title as bill_account_name';
        $columns            .= ',ygt_bill_buyer.title as bill_buyer_name';
        $join               = [
            ['table'=>'ygt_bill_category','field_l'=>'ygt_bill_category.id','field_c'=>'=','field_r'=>'ygt_bill_log.bill_category_id'],
            ['table'=>'ygt_bill_account','field_l'=>'ygt_bill_account.id','field_c'=>'=','field_r'=>'ygt_bill_log.bill_account_id'],
            ['table'=>'ygt_bill_buyer','field_l'=>'ygt_bill_buyer.id','field_c'=>'=','field_r'=>'ygt_bill_log.bill_buyer_id'],
        ];
        $orderby            = ['ygt_bill_log.id','desc'];
        $groupby            = '';
        if($isList){
            $colletion          = self::getList($where,$columns,$limit,$offset,$orderby,$groupby,$join);
            $lists              = $colletion->toArray();
            $weekArr            = self::getWeekTextArr();
            foreach($lists as $key=>$val)
            {
                $day            = date('j',$val['created_at']);
                $week           = date('N',$val['created_at']);
                $dayText        = $day;
                $weekText       = $weekArr[$week];
                $dayWeek        = $dayText.','.$weekText;
                $lists[$key]['day']          = $dayText;
                $lists[$key]['week']         = $weekText;
                $lists[$key]['day_week']    = $dayWeek;
                $lists[$key]['created_hi']  = date('H:i',$val['created_at']);
            }
            $result             = $lists;
        }else{
            $result             = self::getInfo($where,$columns,$orderby,$groupby,$join);
        }
        return $result;
    }
}
